package com.mzy.linear_struct.hot100.回溯;

import java.util.*;

/**
 * @Author: mizy3
 * @Date: 2024/07/26/上午11:23
 * @Description:
 */
public class Pro39组合总和 {

    public List<List<Integer>> combinationSum(int[] candidates, int target) {

        LinkedList<Integer> path = new LinkedList<>();
        List<List<Integer>> ans = new ArrayList<>();

        backtracking(candidates, target, path, 0, ans);

        return ans;
    }

    private void backtracking(int[] candidates, int target, LinkedList<Integer> path, int startIndex, List<List<Integer>> ans) {
        if (target < 0) {
            return;
        }
        if (target == 0) {
            ans.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i < candidates.length; i++) {
            path.addLast(candidates[i]);
            backtracking(candidates, target - candidates[i], path, i, ans);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        System.out.println(new Pro39组合总和().combinationSum(new int[]{2, 3, 6, 7}, 7));
    }
}





























